package com.sky.com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    @Autowired
    DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //保存套餐
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO, Setmeal.class);
        setmealMapper.insert(setmeal);

        //保存套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //关联套餐ID
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });


    }

    /**
     * 根据套餐ID查询套餐+套餐菜品
     *
     * @param id
     * @return
     **/
    @Override
    public SetmealVO getByIdWithDishs(Long id) {
        //查询套餐
        Setmeal setmeal = setmealMapper.getById(id);
        //查询套餐菜品
        List<SetmealDish> dishes = setmealDishMapper.listBySetmealId(id);
        //封装VO返回
        SetmealVO setmealVO = BeanUtil.copyProperties(setmeal, SetmealVO.class);
        setmealVO.setSetmealDishes(dishes);
        return setmealVO;
    }

    /**
     * 套餐修改
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //套餐修改
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO, Setmeal.class);
        setmealMapper.update(setmeal);
        //删除旧的餐品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //添加新的菜品
        setmealDTO.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });

    }


    /**
     * 套餐启售停售
     *
     * @param setmeal
     */
    @Override
    @Transactional
    public void updateStatus(Setmeal setmeal) {
        //起售套餐:需要查看关联菜品是否都是起售
        if (setmeal.getStatus() == StatusConstant.ENABLE) {
            //查询是否关联有停售状态菜品
            Long count = dishMapper.countBySiAndStatus(setmeal.getId(), 0);
            if (count != null && count > 0) {
                throw new BusinessException(500,"套餐关联有未启售菜品，套餐不能起售");
            }
        }
        //修改套餐状态
        setmealMapper.update(setmeal);

    }

    /**
     * 根据分类id查询套餐列表
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public List<Setmeal> paramList(SetmealDTO setmealDTO) {
        return setmealMapper.paramList(setmealDTO);
    }

    //分页
    @Override
    public PageResult getPage(SetmealPageDTO setmealPageDTO) {
        //1. 开启分页
        PageHelper.startPage(setmealPageDTO.getPage(), setmealPageDTO.getPageSize());
        //2. 查询list (条件)
        List<SetmealVO> voList = setmealMapper.getList(setmealPageDTO);
        Page<SetmealVO> page = (Page<SetmealVO>) voList;
        //3.返回分页
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     **/
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //1. 参数校验
        //2. 业务校验(套餐名唯一)
        //3. dto->entity
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO, Setmeal.class);
        setmeal.setStatus(StatusConstant.DISABLE);//禁用
        //4. 保存套餐基本信息
        setmealMapper.insert(setmeal);
        //5. 取出中间表
        List<SetmealDish> dishList = setmealDTO.getSetmealDishes();
        if(ArrayUtil.isNotEmpty(dishList)){
            for (SetmealDish setmealDish : dishList) {
                // 5-1 关联套餐id
                setmealDish.setSetmealId(setmeal.getId());
                // 5-2 保存中间表
                setmealDishMapper.insert(setmealDish);
            }
        }

    }

    // 回显套餐
    @Override
    public SetmealVO getById(Long id) {
       //1. 先查询套餐基本信息
        Setmeal setmeal = setmealMapper.getById(id);
        //2. 在查询中间表信息
        List<SetmealDish> dishList = setmealDishMapper.getListBySetmealId(id);
        //3. 封装vo
        SetmealVO setmealVO = BeanUtil.copyProperties(setmeal, SetmealVO.class);
        setmealVO.setSetmealDishes(dishList);
        //4. 返回vo
        return setmealVO;

    }

    // 修改套餐
    @Transactional
    @Override
    public void updateById(SetmealDTO setmealDTO) {
       // 修改套餐
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO, Setmeal.class);
        setmealMapper.updateById(setmeal);
        //删除就饿得菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        // 添加新的菜品
        setmealDTO.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });


    }

    // 条件查询
    @Override
    public List<SetmealVO> getParamList(SetmealPageDTO setmealPageDTO) {
        return setmealMapper.getList(setmealPageDTO);
    }

    /**
     * 套餐分页查询
     *
     * @param pageDTO
     * @return
     **/
    @Override
    public PageResult page(SetmealPageDTO pageDTO) {
        //开启分页
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getPageSize());
        //2 条件查询
        List<SetmealVO> list = setmealMapper.list(pageDTO);
        PageInfo<SetmealVO> pageInfo = new PageInfo<>(list);
        //返回分页结果
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    // 套餐批量删除
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                // 启售中的套餐不能删除
                throw new BusinessException(200,"启售中的套餐不能删除");
            }
        });
        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });


    }

    @Transactional
    @Override
    public void startOrStop(Setmeal setmeal) {
        //起售套餐:需要擦好看关联菜品是否都是启售
        if(setmeal.getStatus() == StatusConstant.ENABLE){
            //查询是否关联有停售状态菜品
            Long count = dishMapper.countBySiAndStatus(setmeal.getId(), 0);
            if(count != null && count>0){
                throw new BusinessException(200,"套餐关联有未起售菜品，套餐不能起售");
            }
            //修改套餐状态
            setmealMapper.updateById(setmeal);
        }

    }


}
